package com.yc.cloud.clouduser.web;

import com.yc.cloud.clouduser.vo.Result;
import com.yc.cloud.clouduser.vo.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class UserAction {
    @RequestMapping("user")
    public String order(HttpServletRequest request){
        int port=request.getServerPort();
        return "user:"+port;
    }

    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("user-index")
    public String index(){
        String url="http://127.0.0.1:80/index";
        String object=restTemplate.getForObject(url,String.class);
        return "user: "+object;
    }

    @RequestMapping("user-order")
    public String order(){
        String url="http://127.0.0.1:8002/order";
        String object=restTemplate.getForObject(url,String.class);
        return "user: "+object;
    }

    @RequestMapping("user-index1")
    public String index1(){
        String url="http://cloud-index/index";
        String object=restTemplate.getForObject(url,String.class);
        return "user: "+object;
    }

    @RequestMapping("user-order1")
    public String order1(){
        String url="http://cloud-order/order";
        String object=restTemplate.getForObject(url,String.class);
        return "user: "+object;
    }

    @RequestMapping("add")
    public int add(int a,int b){
        return a+b;
    }

    @RequestMapping("div")
    public Result div(int a, int b){
        if(b==0){
            return new Result(0,"除数不能为0",null);
        }else{
            return new Result(1,"运算成功",a/b);
        }
    }

    @RequestMapping("reg")
    public Result reg(@RequestBody User user){
        user.setAge(user.getAge()+1);
        return new Result(1,"注册成功",user);
    }

    @RequestMapping("div1{num}")
    public double div1(@PathVariable Integer num){
        return 1D/num;
    }

    @RequestMapping("sendVcode")
    public String sendVcode(){
        return "验证码已通过手机发送成功";
    }
}
